
### WRITE HEADER OF TEX FILE ###
proc produce_head_latex {} {
	global title authors

#	printline {\documentclass[11pt,a4paper,normalheadings,twoside,DIV12]{scrartcl}}
	printline {\documentclass[a4paper,normalheadings]{scrartcl}}
	printline {\usepackage[T1]{fontenc}}
	printline {\linespread{1.05} % avoid foot-note markers to mess up the line spacing}
	printline {\usepackage[norule,perpage]{footmisc}}
	printline {\usepackage{lmodern}}
	printline {\usepackage{ucs}}
	printline {\usepackage[utf8x]{inputenc}}
	printline {\usepackage[english]{babel}}
	printline {\usepackage[small,bf,hang]{caption}}
	printline {\usepackage{epsfig}}
	printline {\emergencystretch = 10pt}
	printline {\clubpenalty = 10000}
	printline {\widowpenalty = 300}
	printline {\displaywidowpenalty = 10000}
	printline {\usepackage{fancyhdr}}
	printline {\usepackage{palatino}}
	printline {\usepackage{helvet}}
	printline {\usepackage{courier}}
	printline {\usepackage{amsmath}}
	printline {\usepackage{amssymb}}
	printline {\usepackage{graphicx}}
	printline {\usepackage{color}}
	printline {\usepackage[scaled]{beramono}}
	printline "\\usepackage\[pdftex,pdfauthor={$authors},pdftitle={$title},pagebackref=true,colorlinks=true,linkcolor=darkblue,citecolor=darkblue,urlcolor=darkblue\]{hyperref}"
	printline {\usepackage{enumitem} % for colored items as used in spec }
	printline {\usepackage{tabularx} % for tables with paragraphs as used in spec }
	printline {\input{img/tikz-preamble.tex}}
	printline {\urlstyle{same}}

	printline {\begin{document}}

	printline {\input{img/tikz-common.tex}}

	printline {\definecolor{lightgray}{gray}{0.5}}
	printline {\definecolor{darkblue}{rgb}{0.1,0,0.55}}

	printline {\begin{titlepage}}
	printline {\thispagestyle{empty}}
	printline {\input{img/title.tikz}}
	printline {\end{titlepage}}
	
	printline "\\title{[out_latex $title]}"
	printline "\\author{[out_latex $authors]}"

	printline {\pagestyle{fancy}}
	printline {\fancyhf{}}
	printline {\fancyhead[RE]{\slshape \uppercase{\leftmark}}}
	printline {\fancyhead[LO]{\slshape \uppercase{\rightmark}}}
	printline {\fancyfoot[C]{\thepage \\ \vspace{1cm} \epsfig{file=img/genode_labs,angle=0,height=0.5cm}}}
	printline {\renewcommand{\footrulewidth}{0pt}}
	printline {\tableofcontents}
}


### WRITE TAIL OF TEX FILE ###
proc produce_tail_latex {} {
	printline "\\end{document}"
}


### CHAPTER ###
proc process_chapter_latex {txtblock} {
	global curr_chapter references
	global chapter_tag
	set title [linetxt [lindex $txtblock 0]]
	set curr_chapter $references($title,index)
	printline ""
	printline "%     -+*|\[ [string toupper $title] \]|*+-\n"
	printline "\\clearpage"
	printline "\\$chapter_tag[section_env_latex $title]"
	handle_txtblock chapter [lrange $txtblock 2 end]
}


### SECTION ###
proc process_section_latex {txtblock} {
	set title [linetxt [lindex $txtblock 0]]
	global section_tag
	printline ""
	printline "%     -+*|\[ [string toupper $title] \]|*+-\n"
	printline "\\clearpage"
	printline "\\$section_tag[section_env_latex $title]"
	handle_txtblock section [lrange $txtblock 2 end]
}


### ANNOTATION ###
proc process_annotation_latex {txtblock} {
	set new_txtblock {}
	foreach txtline $txtblock {
		set txtline [lineregsub {^\| ?} $txtline ""]
		lappend new_txtblock $txtline
	}
	printline "\\vspace{0.2cm}{\\setlength{\\parindent}{0cm} \\colorbox\[gray\]{0.9}{\\parbox{10cm}{ \\color{lightgray} \\footnotesize \\it"
	handle_txtblock annotation $new_txtblock
	printline "}}}\\vspace{0.2cm}"
}


### VERBATIM  ###
proc process_verbatim_latex {txtblock} {
	while {[lindex $txtblock end] == ""} {
		set txtblock [lrange $txtblock 0 [expr [llength $txtblock]-2]]
	}

	#
	# For the output of verbatim text, we use 'puts' rather than
	# 'printline' because 'printline' would indent the text, which
	# is not desired within a verbatim environment.
	#
	printline "\\begin{flushleft}"
	printline "\\begin{tikzpicture}"
	printline "\\node\[font=\\small] (verbatim) {"
	printline "\\begin{lstlisting}"
	foreach txtline $txtblock {
		set txt [linetxt $txtline]
		regsub     {^\!} $txt ""   txt
		regsub -all {\t} $txt "  " txt
		puts "$txt"
	}
	printline "\\end{lstlisting}"
	printline "};"
	printline "\\path\[very thick, draw=black!5\] (verbatim.north west) -- (verbatim.south west);"
	printline "\\end{tikzpicture}"
	printline "\\end{flushleft}"
}
